//
//  HDEditPasswordViewController.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD

class HDEditPasswordViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {
    
    var tableView = UITableView()
    var finishButton = HDNextStepButton(title: "完成")
    
    let editPWTitle = ["新密码","确认密码"]
    let PlaceHolder = ["请输入新密码","请再输入一次"]
    
    var oldPwd = ""
    var newPwd = ""
    var newAgainPwd = ""
    var cell: HDEditPassWordCell?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        title = "修改密码"
        
        tableView.backgroundColor = UIColor(hex: 0xf8f8f8)
        view.backgroundColor = UIColor(hex: 0xf8f8f8)
        setupView()
    }
    //MARK: tableView和button
    func setupView(){
        
        tableView = UITableView(frame: CGRect.zero, style: .plain)
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo(187)
        }
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.isScrollEnabled = false
        
        finishButton.addTarget(self, action: #selector(finishButtonAction), for: .touchUpInside)
        self.view .addSubview(finishButton)
        finishButton.snp.makeConstraints { (make) in
            make.left.equalTo(37)
            make.right.equalTo(-37)
            make.top.equalTo(tableView.snp.bottom).offset(50)
            make.height.equalTo(40)
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView(frame: CGRect(x: 0,y: 0,width: AppConstant.screenWidth,height: 20))
        headView.backgroundColor = UIColor(hex: 0xf8f8f8)
        return headView
    }
    
    func finishButtonAction() {
        if oldPwd.characters.count == 0 && newPwd.characters.count == 0 && newAgainPwd.characters.count == 0{
            SVProgressHUD.showError("请输入密码")
            return
        }
        if oldPwd.characters.count < 6 || oldPwd.characters.count > 16 {
            SVProgressHUD.showError("请输入6～16位密码")
            return
        }
        if newPwd.characters.count < 6 || newPwd.characters.count > 16 {
            SVProgressHUD.showError("请输入6～16位密码")
            return
        }
        if newAgainPwd.characters.count < 6 || newAgainPwd.characters.count > 16 {
            SVProgressHUD.showError("请输入6～16位密码")
            return
        }
        if newPwd != newAgainPwd {
            SVProgressHUD.showError("您两次输入的密码不一致")
            return
        }
        HDApiManagerHandler.requestEditpwd(HDAccountHandler.token!, oldptxt: oldPwd.md5(), newptxt: newPwd.md5(), success: { (dict) in
            if dict["status"]?.int32Value == 1 {
                SVProgressHUD.showSuccess(dict["msg"] as! String)
                self.navigationController?.popViewController(animated: true)
            } else {
                SVProgressHUD.showError(dict["msg"] as! String)
            }
            }) { (error) in
                
        }
    }
    
    //收回键盘
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    //MARK: tableView 的delegate
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }else {
            return 2
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 49
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         cell = HDEditPassWordCell(style: .default, reuseIdentifier: "HDEditPassWordCell",cellType: twoCellType.editPW)
        if indexPath.section == 1 && indexPath.row == 0 {
            cell?.line2.isHidden = true
        }
        
        if indexPath.section == 0 {
            cell!.leftTitle.text = "原密码"
            cell!.inputTextField.placeholder = "请输入原密码"
            cell?.inputTextField.isSecureTextEntry = true
        }else {
            cell!.leftTitle.text = editPWTitle[indexPath.row]
            cell!.inputTextField.placeholder = PlaceHolder[indexPath.row]
            cell?.inputTextField.isSecureTextEntry = true
        }
            cell!.inputTextField.delegate = self
        
        if indexPath.section == 0 {
            cell!.inputTextField.addTarget(self, action: #selector(oldPwdTextFieldChanged(_:)), for: .editingChanged)
        }else if indexPath.section == 1 {
            if indexPath.row == 0 {
                cell!.inputTextField.addTarget(self, action: #selector(newPwdTextFieldChanged(_:)), for: .editingChanged)
            }else {
                cell!.inputTextField.addTarget(self, action: #selector(newAgainPwdTextFieldChanged(_:)), for: .editingChanged)
            }
        }
        
        
        cell!.selectionStyle = .none
        return cell!
    }
    
    func oldPwdTextFieldChanged(_ sender: UITextField) {
        if let text = sender.text {
            oldPwd = text
        }
    }
    func newPwdTextFieldChanged(_ sender: UITextField) {
        if let text = sender.text {
            newPwd = text
        }
    }
    func newAgainPwdTextFieldChanged(_ sender: UITextField) {
        if let text = sender.text {
            newAgainPwd = text
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}
